package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.domain.PmsProduct;
import com.ruoyi.system.domain.vo.BatchEntityVo;
import com.ruoyi.system.domain.vo.BatchProductVo;
import com.ruoyi.system.domain.vo.ProductExcel;
import lombok.SneakyThrows;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.PmsBatch;
import com.ruoyi.system.service.IPmsBatchService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 商品批次Controller
 *
 * @author ruoyi
 * @date 2024-11-11
 */
@RestController
@RequestMapping("/system/batch")
public class PmsBatchController extends BaseController
{
    @Autowired
    private IPmsBatchService pmsBatchService;

    /**
     * 查询商品批次列表
     */
    @PreAuthorize("@ss.hasPermi('system:batch:list')")
    @GetMapping("/list")
    public TableDataInfo list(PmsBatch pmsBatch)
    {
        startPage();
        List<PmsBatch> list = pmsBatchService.selectPmsBatchList(pmsBatch);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:batch:list')")
    @PostMapping("/listAll")
    public AjaxResult listAll(PmsBatch pmsBatch)
    {
        List<PmsBatch> list = pmsBatchService.selectPmsBatchList(pmsBatch);
        return success(list);
    }

    @PreAuthorize("@ss.hasPermi('system:batch:list')")
    @GetMapping("/getBatchListAll")
    public AjaxResult getBatchListAll(PmsBatch pmsBatch)
    {
//        List<BatchEntityVo> list = pmsBatchService.getBatchListAll(pmsBatch);
        List<BatchProductVo> list = pmsBatchService.getBatchListVoAll(pmsBatch);

        return success(list);
    }

    /**
     * 导出商品批次列表
     */
    @PreAuthorize("@ss.hasPermi('system:batch:export')")
    @Log(title = "商品批次", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PmsBatch pmsBatch)
    {
        List<PmsBatch> list = pmsBatchService.selectPmsBatchList(pmsBatch);
        ExcelUtil<PmsBatch> util = new ExcelUtil<PmsBatch>(PmsBatch.class);
        util.exportExcel(response, list, "商品批次数据");
    }

    /**
     * 导入商品批次列表
     */
    @SneakyThrows
    @PreAuthorize("@ss.hasPermi('system:batch:import')")
    @Log(title = "导入商品", businessType = BusinessType.EXPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file)
    {
        ExcelUtil<PmsProduct> util = new ExcelUtil<PmsProduct>(PmsProduct.class);
        List<PmsProduct> productExcels = util.importExcel(file.getInputStream());
        return success(productExcels);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<ProductExcel> util = new ExcelUtil<ProductExcel>(ProductExcel.class);
        util.importTemplateExcel(response, "导入商品");
    }




    /**
     * 获取商品批次详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:batch:query')")
    @GetMapping(value = "/{batchId}")
    public AjaxResult getInfo(@PathVariable("batchId") Long batchId)
    {
        return success(pmsBatchService.selectPmsBatchByBatchId(batchId));
    }

//    /**
//     * 新增商品批次
//     */
//    @PreAuthorize("@ss.hasPermi('system:batch:add')")
//    @Log(title = "商品批次", businessType = BusinessType.INSERT)
//    @PostMapping("/addBatchProduct")
//    public AjaxResult addBatchProduct(@RequestBody BatchProductVo batchProductVo)
//    {
//        return toAjax(pmsBatchService.addBatchProduct(batchProductVo));
//    }

    /**
     * 新增商品批次
     */
    @PreAuthorize("@ss.hasPermi('system:batch:add')")
    @Log(title = "商品批次", businessType = BusinessType.INSERT)
    @PostMapping("/addBatchProduct")
    public AjaxResult addBatchProduct(@RequestBody BatchProductVo batchProductVo)
    {
        return toAjax(pmsBatchService.addBatchProduct(batchProductVo));
    }

    /**
     * 修改商品批次
     */
    @PreAuthorize("@ss.hasPermi('system:batch:edit')")
    @Log(title = "商品批次", businessType = BusinessType.INSERT)
    @PutMapping("/updateBatchProduct")
    public AjaxResult updateBatchProduct(@RequestBody BatchProductVo batchProductVo)
    {
        return toAjax(pmsBatchService.updateBatchProduct(batchProductVo));
    }

    /**
     * 查询商品批次
     */
    @PreAuthorize("@ss.hasPermi('system:batch:list')")
    @Log(title = "商品批次", businessType = BusinessType.INSERT)
    @GetMapping("/getBatchProductById/{batchId}")
    public AjaxResult getBatchProductById(@PathVariable("batchId") Long batchId)
    {
        return success(pmsBatchService.getBatchProductById(batchId));
    }



    /**
     * 按批次添加商品
     */
    @PreAuthorize("@ss.hasPermi('system:batch:add')")
    @Log(title = "按批次添加商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PmsBatch pmsBatch)
    {
        return toAjax(pmsBatchService.insertPmsBatch(pmsBatch));
    }

    /**
     * 修改商品批次
     */
    @PreAuthorize("@ss.hasPermi('system:batch:edit')")
    @Log(title = "商品批次", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PmsBatch pmsBatch)
    {
        return toAjax(pmsBatchService.updatePmsBatch(pmsBatch));
    }

    /**
     * 删除商品批次
     */
    @PreAuthorize("@ss.hasPermi('system:batch:remove')")
    @Log(title = "商品批次", businessType = BusinessType.DELETE)
	@DeleteMapping("/{batchIds}")
    public AjaxResult remove(@PathVariable Long[] batchIds)
    {
        return toAjax(pmsBatchService.deletePmsBatchByBatchIds(batchIds));
    }
}
